cmake_minimum_required(VERSION 3.16)

project(ALTCP_TLS_HITLS)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")

if(NOT DEFINED HITLS_DIR)
    set(HITLS_DIR /home/ohui21/zh/code/openhitls/)
endif()

if(NOT DEFINED MBEDTLS_DIR)
    set(MBEDTLS_DIR /home/ohui21/zh/mbedtls/mbedtls-2.28.10)
endif()

set(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(ALTCP_TLS_HITLS_DIR ${ROOT_DIR}/altcp_tls_hitls)
set(LWIP_DIR ${ROOT_DIR}/lwip-2.2.0)
set(LWIP_CONTRIB_DIR ${ROOT_DIR}/lwip-2.2.0/contrib)
set(TESTCODE_DIR ${ROOT_DIR}/testcode)

set(LWIP_INCLUDE_DIRS
    ${LWIP_DIR}/src/include
    ${LWIP_DIR}/src/core
    ${LWIP_CONTRIB_DIR}
    ${LWIP_CONTRIB_DIR}/ports/unix/include
    ${LWIP_CONTRIB_DIR}/ports/unix/port/include
)

# liblwip-xxx.a---------------------------------------------------------------
set(lwipcore_SRCS
    ${LWIP_DIR}/src/core/init.c
    ${LWIP_DIR}/src/core/def.c
    ${LWIP_DIR}/src/core/dns.c
    ${LWIP_DIR}/src/core/inet_chksum.c
    ${LWIP_DIR}/src/core/ip.c
    ${LWIP_DIR}/src/core/mem.c
    ${LWIP_DIR}/src/core/memp.c
    ${LWIP_DIR}/src/core/netif.c
    ${LWIP_DIR}/src/core/pbuf.c
    ${LWIP_DIR}/src/core/raw.c
    ${LWIP_DIR}/src/core/stats.c
    ${LWIP_DIR}/src/core/sys.c
    ${LWIP_DIR}/src/core/altcp.c
    ${LWIP_DIR}/src/core/altcp_alloc.c
    ${LWIP_DIR}/src/core/altcp_tcp.c
    ${LWIP_DIR}/src/core/tcp.c
    ${LWIP_DIR}/src/core/tcp_in.c
    ${LWIP_DIR}/src/core/tcp_out.c
    ${LWIP_DIR}/src/core/timeouts.c
    ${LWIP_DIR}/src/core/udp.c
)
set(lwipcore4_SRCS
    ${LWIP_DIR}/src/core/ipv4/acd.c
    ${LWIP_DIR}/src/core/ipv4/autoip.c
    ${LWIP_DIR}/src/core/ipv4/dhcp.c
    ${LWIP_DIR}/src/core/ipv4/etharp.c
    ${LWIP_DIR}/src/core/ipv4/icmp.c
    ${LWIP_DIR}/src/core/ipv4/igmp.c
    ${LWIP_DIR}/src/core/ipv4/ip4_frag.c
    ${LWIP_DIR}/src/core/ipv4/ip4.c
    ${LWIP_DIR}/src/core/ipv4/ip4_addr.c
)

# APIFILES: The files which implement the sequential and socket APIs.
set(lwipapi_SRCS
    ${LWIP_DIR}/src/api/api_lib.c
    ${LWIP_DIR}/src/api/api_msg.c
    ${LWIP_DIR}/src/api/err.c
    ${LWIP_DIR}/src/api/if_api.c
    ${LWIP_DIR}/src/api/netbuf.c
    ${LWIP_DIR}/src/api/netdb.c
    ${LWIP_DIR}/src/api/netifapi.c
    ${LWIP_DIR}/src/api/sockets.c
    ${LWIP_DIR}/src/api/tcpip.c
)

# Files implementing various generic network interface functions
set(lwipnetif_SRCS
    ${LWIP_DIR}/src/netif/ethernet.c
    ${LWIP_DIR}/src/netif/bridgeif.c
    ${LWIP_DIR}/src/netif/bridgeif_fdb.c
)

set(lwipcontribportunix_SRCS
    ${LWIP_CONTRIB_DIR}/ports/unix/port/netif/tapif.c
    ${LWIP_CONTRIB_DIR}/ports/unix/port/sys_arch.c
    ${LWIP_CONTRIB_DIR}/ports/unix/port/perf.c
)

set(lwipaltcp_tls_SRCS
    ${LWIP_DIR}/src/apps/altcp_tls/altcp_tls_mbedtls.c
    ${LWIP_DIR}/src/apps/altcp_tls/altcp_tls_mbedtls_mem.c
)

include(${ROOT_DIR}/hitls_conf/lwip.cmake)
include(${ROOT_DIR}/mbedtls_conf/lwip.cmake)


# altcp_hitls.a---------------------------------------------------------------
set(altcp_hitls_SRCS
    ${ALTCP_TLS_HITLS_DIR}/src/altcp_tls_hitls.c
)

add_library(altcp_hitls STATIC ${altcp_hitls_SRCS})
target_include_directories(altcp_hitls PRIVATE 
    ${ALTCP_TLS_HITLS_DIR}/include
    ${LWIP_INCLUDE_DIRS}
    ${HITLS_DIR}/include/tls
    ${HITLS_DIR}/include/pki
    ${HITLS_DIR}/include/crypto
    ${HITLS_DIR}/include/bsl
    ${ROOT_DIR}/hitls_conf
)
target_link_libraries(altcp_hitls PRIVATE
    lwip_hitls
    hitls_tls
    hitls_pki
    hitls_crypto
    hitls_bsl
    boundscheck
    dl
)
target_link_directories(altcp_hitls PRIVATE
    ${HITLS_DIR}/build
    ${HITLS_DIR}/platform/Secure_C/lib
)
target_compile_definitions(altcp_hitls PRIVATE
    LWIP_ALTCP
    LWIP_ALTCP_TLS_HITLS
)

# testcode hitls_server---------------------------------------------------------------
add_executable(hitls_server ${TESTCODE_DIR}/altcp_hitls_server.c)
target_link_libraries(hitls_server PRIVATE
    altcp_hitls
    lwip_hitls
    pthread
)
target_include_directories(hitls_server PRIVATE
    ${TESTCODE_DIR}
    ${ALTCP_TLS_HITLS_DIR}/include
    ${LWIP_INCLUDE_DIRS}
    ${HITLS_DIR}/include/crypto
    ${HITLS_DIR}/include/pki
    ${HITLS_DIR}/include/tls
    ${HITLS_DIR}/include/bsl
    ${ROOT_DIR}/hitls_conf
)
target_link_directories(hitls_server PRIVATE
    ${ROOT_DIR}/build
    ${HITLS_DIR}/build
    ${HITLS_DIR}/platform/Secure_C/lib
)

# testcode hitls_client---------------------------------------------------------------
add_executable(hitls_client ${TESTCODE_DIR}/altcp_hitls_client.c)
target_link_libraries(hitls_client PRIVATE
    altcp_hitls
    lwip_hitls
    pthread
)
target_include_directories(hitls_client PRIVATE
    ${TESTCODE_DIR}
    ${ALTCP_TLS_HITLS_DIR}/include
    ${LWIP_INCLUDE_DIRS}
    ${HITLS_DIR}/include/crypto
    ${HITLS_DIR}/include/pki
    ${HITLS_DIR}/include/tls
    ${HITLS_DIR}/include/bsl
    ${ROOT_DIR}/hitls_conf
)
target_link_directories(hitls_client PRIVATE
    ${ROOT_DIR}/build
    ${HITLS_DIR}/build
    ${HITLS_DIR}/platform/Secure_C/lib
)

# testcode hitls_test---------------------------------------------------------------
add_executable(hitls_test ${TESTCODE_DIR}/altcp_hitls_test.c)
target_link_libraries(hitls_test PRIVATE
    altcp_hitls
    lwip_hitls
    pthread
)
target_include_directories(hitls_test PRIVATE
    ${TESTCODE_DIR}
    ${ALTCP_TLS_HITLS_DIR}/include
    ${LWIP_INCLUDE_DIRS}
    ${HITLS_DIR}/include/crypto
    ${HITLS_DIR}/include/pki
    ${HITLS_DIR}/include/tls
    ${HITLS_DIR}/include/bsl
    ${ROOT_DIR}/hitls_conf
)
target_link_directories(hitls_test PRIVATE
    ${ROOT_DIR}/build
    ${HITLS_DIR}/build
    ${HITLS_DIR}/platform/Secure_C/lib
)

# testcode mbedtls_server---------------------------------------------------------------
add_executable(mbedtls_server ${TESTCODE_DIR}/altcp_mbedtls_server.c)
target_link_libraries(mbedtls_server PRIVATE
    lwip_mbedtls
    pthread
)
target_include_directories(mbedtls_server PRIVATE
    ${TESTCODE_DIR}
    ${LWIP_INCLUDE_DIRS}
    ${MBEDTLS_DIR}/include
    ${ROOT_DIR}/mbedtls_conf
)
target_link_directories(mbedtls_server PRIVATE
    ${ROOT_DIR}/build
    ${MBEDTLS_DIR}/build/library
)

# testcode mbedtls_client---------------------------------------------------------------
add_executable(mbedtls_client ${TESTCODE_DIR}/altcp_mbedtls_client.c)
target_link_libraries(mbedtls_client PRIVATE
    lwip_mbedtls
    pthread
)
target_include_directories(mbedtls_client PRIVATE
    ${TESTCODE_DIR}
    ${LWIP_INCLUDE_DIRS}
    ${MBEDTLS_DIR}/include
    ${ROOT_DIR}/mbedtls_conf
)
target_link_directories(mbedtls_client PRIVATE
    ${ROOT_DIR}/build
    ${MBEDTLS_DIR}/build/library
)
